package Netty.buffer;

import java.nio.ByteBuffer;

/**
 * @author caihe
 * @date 2021/11/15 9:44
 */
public class ByteBufferDemo_flip {

    public static void main(String[] args) {

        // 初始化一个大小为4的ByteBuffer
        ByteBuffer byteBuffer = ByteBuffer.allocate(4);
        print(byteBuffer);
        
        // 写入三个字节的数据
        byteBuffer.put((byte) 1);
        byteBuffer.put((byte) 2);
        byteBuffer.put((byte) 3);
        print(byteBuffer);
        System.out.println("*********** flip ********");

        byteBuffer.flip();
        print(byteBuffer);
        
        System.out.println("*********** get ********");
        System.out.println(byteBuffer.get());
        System.out.println(byteBuffer.get());
        print(byteBuffer);
        
        byteBuffer.flip();
        byteBuffer.put((byte) 4);
        print(byteBuffer);

        System.out.println(byteBuffer.get());
        print(byteBuffer);

    }
    
    private static void print(ByteBuffer buffer) {
        System.out.printf("pos: %d, limit: %d, capacity: %d \n", buffer.position(), buffer.limit(), buffer.capacity());
    }
    
    
}
